package shortcircuit.graphics;

import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.event.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import shortcircuit.Display;
import shortcircuit.GameEngine;
import shortcircuit.engine.MainMenu;

/**
 * Contains all of the statistics involved in the game processes such as level,
 * score, and blocks remaining / next blocks depending on which mode is being
 * played.  Additionally, an image at the top of the screen displays the mode name
 * and two buttons at the bottom allow for pausing and quitting of the current mode.
 * 
 * @author Kevin Lewi
 */
public class StatsScreen extends JPanel
{
	GameEngine _Engine;
	private String engineName;
	private JPanel levelPanel;
	ArrayList<JLabel> levelLabels;
	private JPanel scorePanel;
	ArrayList<JLabel> scoreLabels;
	private JPanel highScorePanel;
	ArrayList<JLabel> highScoreLabels;
	private JPanel blocksPanel;
	ArrayList<JLabel> blocksLabels;
	private JPanel movesPanel;
	ArrayList<JLabel> movesLabels;
	private JPanel powerupsPanel;
	public ArrayList<JLabel> powerupsLabels;
	private JLabel scoreLabel = new JLabel("");
	private JPanel blockQueuePanel = new JPanel();
	private JButton pauseButton;
	private JButton quitButton;
	public int labelSelected = -1;

	/**
	 * Organizes all of the elements of the StatsScreen.
	 * 
	 * @author Kevin Lewi
	 */
	public StatsScreen(GameEngine theEngine)
	{	
		addMouseListener(new MouseInput());
		addMouseMotionListener(new MouseInput());
		pauseButton = new JButton(new ImageIcon(Display.pauseImage));
		quitButton = new JButton(new ImageIcon(Display.quitStatImage));
		pauseButton.setBorder(null);
		pauseButton.setContentAreaFilled(false);
		quitButton.setBorder(null);
		quitButton.setContentAreaFilled(false);

		_Engine = theEngine;
		engineName = _Engine.getClass().getName();

		levelPanel = new JPanel();
		levelPanel.setOpaque(false);
		levelPanel.setSize(300, 300);
		levelPanel.setLocation(-45, 70);
		levelLabels = new ArrayList<JLabel>();
		scorePanel = new JPanel();
		scorePanel.setOpaque(false);
		scorePanel.setSize(300, 300);
		scorePanel.setLocation(-45, 120);
		scoreLabels = new ArrayList<JLabel>();
		highScorePanel = new JPanel();
		highScorePanel.setOpaque(false);
		highScorePanel.setSize(300, 300);
		highScorePanel.setLocation(-45, 170);
		highScoreLabels = new ArrayList<JLabel>();
		blocksPanel = new JPanel();
		blocksPanel.setOpaque(false);
		blocksPanel.setSize(300, 300);
		blocksPanel.setLocation(-45, 170);
		blocksLabels = new ArrayList<JLabel>();
		movesPanel = new JPanel();
		movesPanel.setOpaque(false);
		movesPanel.setSize(300, 300);
		movesPanel.setLocation(-45, 120);
		movesLabels = new ArrayList<JLabel>();
		powerupsPanel = new JPanel();
		powerupsPanel.setOpaque(false);
		powerupsPanel.setLayout(null);
		powerupsPanel.setSize(300, 300);
		if(engineName.contains("Puzzle"))
			powerupsPanel.setLocation(0, 200);
		if(engineName.contains("Endless"))
			powerupsPanel.setLocation(0, 220);

		setOpaque(false);

		setLayout(null);

		JLabel modeName = new JLabel();
		if(engineName.contains("Puzzle"))
			modeName = new JLabel(new ImageIcon(Display.puzzleModeImage));
		if(engineName.contains("Endless"))
			modeName = new JLabel(new ImageIcon(Display.endlessModeImage));
		modeName.setSize(300, 300);
		modeName.setLocation(-45, -120);
		
		pauseButton.addActionListener(new ActionInput());
		quitButton.addActionListener(new ActionInput());

		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setOpaque(false);
		buttonPanel.setSize(300, 300);
		buttonPanel.setLocation(-45, 350);
		buttonPanel.add(pauseButton);
		buttonPanel.add(quitButton);


		add(modeName);
		add(levelPanel);
		if(engineName.contains("Endless"))
		{
			add(scorePanel);
			add(highScorePanel);
		}
		if(engineName.contains("Puzzle"))
		{
			add(blocksPanel);
			add(movesPanel);
		}
		add(buttonPanel);
		add(powerupsPanel);
		setVisible(true);
		setLocation(0, 0);
		setSize(200,420);
		update();
	}

	/**
	 * Updates the statsscreen display.
	 * 
	 * @author David Li
	 */
	public void update(){
		levelPanel.removeAll();
		levelLabels = Display.generateLabels(_Engine.getLevel());
		levelPanel.add(new JLabel(new ImageIcon(Display.levelStatsImage)));
		for(int i = 0; i < levelLabels.size(); i++)
		{
			levelPanel.add(levelLabels.get(i));
		}

		scorePanel.removeAll();
		scoreLabels = Display.generateLabels(_Engine.getScore());
		scorePanel.add(new JLabel(new ImageIcon(Display.scoreStatsImage)));
		for(int i = 0; i < scoreLabels.size(); i++)
		{
			scorePanel.add(scoreLabels.get(i));
		}
		
		highScorePanel.removeAll();
		highScoreLabels = Display.generateLabels(_Engine.getHighScore());
		highScorePanel.add(new JLabel(new ImageIcon(Display.highScoreImage)));
		for(int i = 0; i < highScoreLabels.size(); i++)
		{
			highScorePanel.add(highScoreLabels.get(i));
		}
		
		
		powerupsPanel.removeAll();
		JLabel powerUpsImageLabel = new JLabel(new ImageIcon(Display.powerUpsImage));
		powerUpsImageLabel.setLocation(0, 0);
		powerUpsImageLabel.setSize(200, 100);
		powerupsPanel.add(powerUpsImageLabel);
		powerupsLabels = new ArrayList<JLabel>();
		powerupsLabels.add(new JLabel(/*new ImageIcon(Display.powerUps[0])*/));
		powerupsLabels.add(new JLabel(/*new ImageIcon(Display.powerUps[0])*/));
		powerupsLabels.add(new JLabel(/*new ImageIcon(Display.powerUps[0])*/));
		for(int i = 0; i < powerupsLabels.size(); i++)
		{
			if(_Engine.powerUps.get(i).getImage() != null)
			{
				if(i == labelSelected)
					powerupsLabels.set(i, new JLabel(new ImageIcon(_Engine.powerUps.get(i).getSelectedImage())));
				else
					powerupsLabels.set(i, new JLabel(new ImageIcon(_Engine.powerUps.get(i).getImage())));
			}
		}
		for(int i = 0; i < powerupsLabels.size(); i++)
		{
			try
			{
			powerupsLabels.get(i).setLocation(40 * i, 60);
			powerupsLabels.get(i).setSize(40, 40);
			powerupsPanel.add(powerupsLabels.get(i));
			}
			catch(IndexOutOfBoundsException e) {}
		}
		

		if(engineName.contains("Puzzle"))
		{

			blocksPanel.removeAll();
			blocksLabels = Display.generateLabels(_Engine.getBlocksRemaining());
			blocksPanel.add(new JLabel(new ImageIcon(Display.blocksRemainingImage)));
			for(int i = 0; i < blocksLabels.size(); i++)
			{
				blocksPanel.add(blocksLabels.get(i));
			}

			movesPanel.removeAll();
			scoreLabels = Display.generateLabels(_Engine.getMovesLeft());
			movesPanel.add(new JLabel(new ImageIcon(Display.movesLeftImage)));
			for(int i = 0; i < scoreLabels.size(); i++)
			{
				movesPanel.add(scoreLabels.get(i));
			}
		}
		paintAll(getGraphics());
	}

	private class MouseInput extends MouseAdapter implements MouseMotionListener
	{
		private Point cursorPoint = new Point();
		
		public MouseInput()
		{

		}
		
		public void mouseDragged(MouseEvent e) {}
		
		public void mouseMoved(MouseEvent e)
		{
			cursorPoint = new Point(e.getPoint().x + 240, e.getPoint().y + 15);
		}
		
		public void mousePressed(MouseEvent e)
		{
			cursorPoint = new Point(e.getPoint().x + 240, e.getPoint().y + 15);
			_Engine.gameScreen.cursorPoint = new Point();
			for(int i = 0; i < _Engine.powerUps.size() && i < powerupsLabels.size(); i++)
			{
				if(cursorPoint.x - powerupsLabels.get(i).getLocationOnScreen().x + _Engine._Display.getLocation().x <= 40 && 
						cursorPoint.x - powerupsLabels.get(i).getLocationOnScreen().x + _Engine._Display.getLocation().x >= 0 && 
						cursorPoint.y - powerupsLabels.get(i).getLocationOnScreen().y + _Engine._Display.getLocation().y <= 40 && 
						cursorPoint.y - powerupsLabels.get(i).getLocationOnScreen().y + _Engine._Display.getLocation().y >= 0)
				{
					if(_Engine.gameScreen.draggedPowerUpImage != _Engine.powerUps.get(i).getImage())
					{
						_Engine.gameScreen.draggedPowerUpImage = _Engine.powerUps.get(i).getImage();
						labelSelected = i;
						break;
					}
					else
					{
						_Engine.gameScreen.draggedPowerUpImage = null;
						labelSelected = -1;
					}
				}
			}
			update();
		}
	}
	
	
	public class ActionInput implements ActionListener{
		public ActionInput(){}

		public void actionPerformed(ActionEvent e){
			if(e.getSource() == pauseButton){
				_Engine.gamePulse.setInterrupted(!_Engine.gamePulse.isInterrupted());
			}
			else{
				_Engine._Display.clearScreens();
				new MainMenu(_Engine._Display);
			}
		}
	}
}
